#
# 从中国农业农村信息网(http://www.agri.cn/)获取舆情信息
# 该网站有防护措施，仅获取第一页数据（15条）
#

import sys
import io
import requests
import re
import math
import random
import string
import base64
import hashlib
import time

from datetime import datetime
from bs4 import BeautifulSoup
import CapturerDb


def waitSeconds(second):
    time.sleep(second)

def generateRandomString(length):
    # 字符集包括小写字母、大写字母和数字
    letters = string.ascii_letters + string.digits
    return ''.join(random.choice(letters) for _ in range(length))

def generateRandomCookies():
    timestamp = str(int(time.time()))
    md5Hash = hashlib.md5()
    md5Hash.update(timestamp.encode('utf-8'))
    md5=md5Hash.hexdigest()
    signtrue = ('%s,%s' % (timestamp, md5))
    return str(base64.b64encode(signtrue.encode('utf-8')), 'utf-8')

def parsePopularFeelings(newsList):
    divIndex = 0
    for div in newsList:
        #print(item.text.strip()) 
        if divIndex > 1: 
            break
        
        liList = div.find_all('li')
        for li in liList:
            a = li.find('a')
            span = li.find('span')
            
            title = a['title']
            href = a['href']
            dateText = str(datetime.now().year) + '-' + span.text
            
            url = 'http://www.agri.cn' + href[1:]
            
            CapturerDb.updatePopularFeelings('中国农业农村信息网', url, '农业要闻', dateText, '正面', '', title)
            
            #print(title, href, dateText)
        
        divIndex += 1

#获取舆情信息
def getPopularFeelings():
    print('开始获取中国农业农村信息网舆情信息...')
    
    url = f'http://www.agri.cn'
    
    response = requests.get(url)#, headers = headers, cookies=cookies)
    response.encoding = 'utf-8'
    if response.status_code == 200:
        #print(response.text)
        
        soup = BeautifulSoup(response.text, 'html.parser')

        #获取农业要闻
        newsList = soup.find_all(class_='news_list')

        #解析数据
        parsePopularFeelings(newsList)

        
        print('获取中国农业农村信息网舆情信息完成!')
    else:
        print('获取中国农业农村信息网舆情信息失败!')

#测试
#getPopularFeelings()